package com.gopay.bis.web.controller.tpas;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gopay.common.constants.bank.tpas.TpasInterfaceEnum;
import com.gopay.common.constants.bank.tpas.TransTypeConstants;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.TpasResult;
import com.gopay.common.domain.bank.tpas.mwa.FlowMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.TpasChargeInteractRemoteService;

@Controller
@Scope("prototype")
public class TpasController {
    
    @Resource(name="bisTpasChargeService")
    private TpasChargeInteractRemoteService bisTpasChargeService;

    @RequestMapping(value = "/tpas/mobiInfo.shtml")
    protected String mobiInfo(HttpServletRequest request,HttpServletResponse response) throws GopayException {
        
        String parameter = request.getParameter("mobiNo");
        TpasMessage msg = new MobileMessage();
        msg.setOuterCustID(parameter);
        msg.setTransType(IntTxnCd._01611.value);
        msg.setInterfaceCode(TpasInterfaceEnum.MOBINFO);
        TpasResult mobileBelong = bisTpasChargeService.mobileBelong(msg);
        Object defaultModel = mobileBelong.getDefaultModel();
        return ToStringBuilder.reflectionToString(mobileBelong,ToStringStyle.DEFAULT_STYLE)
                + "model = " + ToStringBuilder.reflectionToString(defaultModel,ToStringStyle.DEFAULT_STYLE);
    }
    
    @RequestMapping(value = "/tpas/mobileCharge.shtml")
    protected String mobileCharge(HttpServletRequest request,HttpServletResponse response) throws GopayException {
        
        String parameter = request.getParameter("mobile");
        MobileMessage msg = new MobileMessage();
        msg.setOuterCustID(parameter);
        msg.setTransType(IntTxnCd._01611.value);
        msg.setInterfaceCode(TpasInterfaceEnum.MOBILECHARGE);
        msg.setOrderId(request.getParameter("orderid"));
        msg.setCardId(request.getParameter("cardid"));
        msg.setCardNum(request.getParameter("cardnum"));
        msg.setAmount(new BigDecimal(request.getParameter("amount")));
        TpasResult result = bisTpasChargeService.mobileCharge(msg);
        Object defaultModel = result.getDefaultModel();
        System.out.println(defaultModel);
        return ToStringBuilder.reflectionToString(result,ToStringStyle.DEFAULT_STYLE)
                + "model = " + ToStringBuilder.reflectionToString(defaultModel,ToStringStyle.DEFAULT_STYLE);
    }
    
    @RequestMapping(value = "/tpas/flowCharge.shtml")
    protected String flowCharge(HttpServletRequest request,HttpServletResponse response) throws GopayException {
        
        String parameter = request.getParameter("mobile");
        FlowMessage msg = new FlowMessage();
        msg.setOuterCustID(parameter);
        msg.setTransType(IntTxnCd._01612.value);
        msg.setInterfaceCode(TpasInterfaceEnum.FLOWCHARGE);
        msg.setOrderId(request.getParameter("orderid"));
        msg.setPerValue(request.getParameter("perValue"));
        msg.setFlowValue(request.getParameter("flowValue"));
        msg.setRange(request.getParameter("range"));
        msg.setEffectStartTime(request.getParameter("effectStartTime"));
        msg.setEffectTime(request.getParameter("effectTime"));
        msg.setAmount(new BigDecimal(request.getParameter("amount")));
        TpasResult result = bisTpasChargeService.flowCharge(msg);
        Object defaultModel = result.getDefaultModel();
        System.out.println(defaultModel);
        return ToStringBuilder.reflectionToString(result,ToStringStyle.DEFAULT_STYLE)
                + "model = " + ToStringBuilder.reflectionToString(defaultModel,ToStringStyle.DEFAULT_STYLE);
    }
    
    @RequestMapping(value = "/tpas/oilCardInfo.shtml")
    protected String oilCardInfo(HttpServletRequest request,HttpServletResponse response) throws GopayException {
        
        OilCardMessage msg = new OilCardMessage();
        msg.setTransType(IntTxnCd._01613.value);
        msg.setInterfaceCode(TpasInterfaceEnum.OILCARDINFO);
        
        msg.setOuterCustID(request.getParameter("cardNo"));
        msg.setCardType(request.getParameter("cardType"));
        TpasResult result = bisTpasChargeService.oilCardInfo(msg);
        Object defaultModel = result.getDefaultModel();
        System.out.println(defaultModel);
        return ToStringBuilder.reflectionToString(result,ToStringStyle.DEFAULT_STYLE)
                + "model = " + ToStringBuilder.reflectionToString(defaultModel,ToStringStyle.DEFAULT_STYLE);
    }
    
    @RequestMapping(value = "/tpas/oilCardCharge.shtml")
    protected String oilCardCharge(HttpServletRequest request,HttpServletResponse response) throws GopayException {
        
        OilCardMessage msg = new OilCardMessage();
        msg.setTransType(IntTxnCd._01613.value);
        msg.setInterfaceCode(TpasInterfaceEnum.OILCARDCHARGE);
        
        msg.setCardId(request.getParameter("cardId"));
        msg.setCardNum(request.getParameter("cardNum"));
        msg.setOrderId(request.getParameter("orderid"));
        msg.setOuterCustID(request.getParameter("cardNo"));
        msg.setHolderMobile(request.getParameter("gasCardTel"));
        msg.setAmount(new BigDecimal(request.getParameter("amount")));
        TpasResult result = bisTpasChargeService.oilCardCharge(msg);
        Object defaultModel = result.getDefaultModel();
        System.out.println(defaultModel);
        return ToStringBuilder.reflectionToString(result,ToStringStyle.DEFAULT_STYLE)
                + "model = " + ToStringBuilder.reflectionToString(defaultModel,ToStringStyle.DEFAULT_STYLE);
    }
    
    @RequestMapping(value = "/tpas/queryStatus.shtml")
    protected String queryStatus(HttpServletRequest request,HttpServletResponse response) throws GopayException {
        
        TpasMessage msg = new TpasMessage();
        msg.setOrderId(request.getParameter("orderid"));
        msg.setBankOrderId(msg.getOrderId());
        msg.setTransType(request.getParameter("transType"));
        TpasResult result = bisTpasChargeService.queryOrderStatus(msg);
        Object defaultModel = result.getDefaultModel();
        System.out.println(defaultModel);
        return ToStringBuilder.reflectionToString(result,ToStringStyle.DEFAULT_STYLE)
                + "model = " + ToStringBuilder.reflectionToString(defaultModel,ToStringStyle.DEFAULT_STYLE);
    }
}
